/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.data.controller.ProgencyCtlr;
import org.gov.vis.data.controller.ProgencyCtlrImpl;
import org.gov.vis.data.dto.ProgencyDto;
import org.gov.vis.data.gui.AddProgencyDialog;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;

/**
 * @author Vishwanath
 *
 */
public class ProgencyListener implements ActionListener,BaseListener {



	private AddProgencyDialog progencyDialog;
	private ProgencyCtlr progencyCtlr = new ProgencyCtlrImpl();

	private ErrorsDisplayJPanel errorMsgPanel;
	final private ResourceBundle rb = Util.getResources();
	private ProgencyDto progencyDto;

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */

	public ProgencyListener(AddProgencyDialog progencyDialog){

		this.progencyDialog = progencyDialog;
	}
	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		try{
			//errorMsgPanel = new ErrorsDisplayJPanel(progencyDialog.getjPanel1());
			//errorMsgPanel.clearMsgPanel();

			if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_save"))
					|| actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){
				//Method called to either Add or Edit the Progency detprogencyls
				addOrEditProgency();
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_view"))){			
				//Method called to view the details of Progency Object based on the passed progencyId
				viewProgency(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
				//Method called to delete the Progency Object based on the passed progencyId.
				deleteProgency(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_reset"))){
				//Method called to clear the screen data
				clear();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}//end actionPerformed


	/**
	 * Method called to view the Progency Details
	 * @param _progencyId
	 */
	private void viewProgency(final long  _progencyId) {
		// TODO Auto-generated method stub
		try{
			final ProgencyDto _progencyDto = progencyCtlr.getProgency(_progencyId);
			setDtoDetailsToGui(_progencyDto);
			progencyDialog.validate();
			progencyDialog.repaint();
		}catch(Exception e){
			e.printStackTrace();
		}
	}//end viewProgency
	/**
	 * Method called to either Add or Edit the Progency detprogencyls
	 */
	private void addOrEditProgency(){
		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(progencyDialog);

		try{
			//Method called to Add or Edit the Progency
			/*progencyCtlr.addOrEditProgency(progencyDto);
			//List<String> successMsgLst = new ArrayList<String>();
		//	successMsgLst.add(rb.getString(GuiConstants.MASTER_ADD_OTHER_SUCCESS));
			//errorMsgPanel.setSuccessMessages(successMsgLst);
*/
			progencyCtlr.addOrEditProgency(progencyDto);
			JOptionPane.showMessageDialog(progencyDialog, rb.getString(GuiConstants.DATA_ADDPROGENCY_SUCCESS));
			//progencyDialog.dispose();
			
			//Method called to clear the screen data
			clear();
		}catch(Exception e){
			List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = ProgencyCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;

					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_code").toUpperCase())){
						progencyDialog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_frmcode").toUpperCase())){
						progencyDialog.getLblFarmerCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_frmcode").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_frmname").toUpperCase())){
						progencyDialog.getLblFarmerName().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_frmname").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_nocalving").toUpperCase())){
						progencyDialog.getLblNoOfCalving().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_nocalving").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_noaidoneconception").toUpperCase())){
						progencyDialog.getLblNoAIDoneConc().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_noaidoneconception").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_prntg").toUpperCase())){
						progencyDialog.getLblPercentage().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_prntg").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_prvlctyldltrs").toUpperCase())){
						progencyDialog.getLblPrevVacYldLtrs().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_prvlctyldltrs").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_prtmlkyeldpday").toUpperCase())){
						progencyDialog.getLblPrsntMlkYldLtrs().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_prtmlkyeldpday").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					/*if(_exceptions.containsKey(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase())){
						progencyDialog.getLblw.setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase())){
						progencyDialog.getLblBrwBrtWgt().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase());
						errorMsgLst.add(errMsg);
					}
*/					if(_exceptions.containsKey(rb.getString("dialog_label_prgy_dmmilk").toUpperCase())){
						//progencyDialog.getLbl.setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prgy_dmmilk").toUpperCase());
						errorMsgLst.add(errMsg);
					}

				//	errorMsgPanel.setErrorMessages(errorMsgLst);					
				} else {
					//log.debug(e.getMessage(), e);
				}
			}
			else{
				errorMsgLst.add(e.getMessage());
				//errorMsgPanel.setErrorMessages(errorMsgLst);
			}
			String errorMessages = "";
			Iterator<String> itr = errorMsgLst.iterator();
			String errMsg = null;
			while(itr.hasNext()){
				errMsg = itr.next();
				errorMessages += errMsg;
				
				if(!errMsg.contains("\n")){
					errorMessages += "\n";
				}
			}
			 JOptionPane.showMessageDialog(progencyDialog, errorMessages);
		}		
	}//end addOrEditProgency

	/**
	 * Method called to delete the Progency Object based on the passed progencyId
	 * @param _progencyId
	 */
	private void deleteProgency(final long  _progencyId){
		try{
			progencyCtlr.deleteProgency(_progencyId);
		}catch(Exception e){
			e.getMessage();
		}
	}//end deleteProgency

	/**
	 * Method called to clear the screen data
	 */
	public void clear() {
		// TODO Auto-generated method stub

		//set the Field's data to empty values
		progencyDialog.getTxtFldCode().setText("");
		//progencyDialog.getTxtFldG.setText("");
		progencyDialog.getTxtBxBreed().setText("");
		progencyDialog.getTxtBxBreedType().setText("");
		progencyDialog.getTxtBxBullNo().setText("");
		progencyDialog.getDtChoCalfDateOfBirth().setDate(null);
		progencyDialog.getTxtBxColor().setText("");
		progencyDialog.getTxtBxDistrict().setText("");
		progencyDialog.getTxtBxFarmerCode().setText("");
		progencyDialog.getTxtBxFarmerName().setText("");
		progencyDialog.getTxtBxMandal().setText("");
		//progencyDialog.getTxtFldGrwmthWgt().setText("");
		progencyDialog.getTxtNoOfAiDoneConc().setText("");
		progencyDialog.getTxtBxNoCalving().setText("");
		progencyDialog.getTxtBxPercentage().setText("");
		progencyDialog.getTxtBxPrsntMilkYldLtrs().setText("");
		progencyDialog.getTxtBxPrvVactYldLtrs().setText("");
		progencyDialog.getTxtBxRelationType().setText("");
		progencyDialog.getCombxSexType().setSelectedIndex(0);
		progencyDialog.getTxtBxTagNo().setText("");
		progencyDialog.getTxtBxVillage().setText("");
		progencyDialog.getTxtBxSirBreed().setText("");
		progencyDialog.getTxtBxSiresMilkYieldLtrs().setText("");
		//progencyDialog.getDtChoDateOfDeworm.setDate(null);
		//progencyDialog.getDtChoDateOfDeticking().setDate(null);
		//progencyDialog.getDtChoDateOfVaccination().setDate(null);
		AddProgencyDialog.tempFollowUpLst.clear();
		progencyDialog.refreshScreen();
		//Method called to set the Field's
		
		//Labels to default Black color
		clearGUILabelColors(blackColor);


		//ResultPanel
		//	progencyDialog.getLblResultOrg().setText("");
		//progencyDialog.getLblrRsultCode().setText("");

	}//end clear

	/**
	 * Method called to read the data from GUI and set them to DTO Object 
	 * @param _guiObject
	 */
	@Override
	public void setDtoDetailsToGui(Object object) {
		// TODO Auto-generated method stub
		final ProgencyDto _progencyDto = (ProgencyDto)object;
		progencyDialog.getTxtFldCode().setText(_progencyDto.getCode());
		//progencyDialog.getTxtFldGrwBtWgt().setText(_progencyDto.getBirthWeight());
		progencyDialog.getTxtBxBreed().setText(_progencyDto.getBreed());
		progencyDialog.getTxtBxBreedType().setText(_progencyDto.getBreedType());
		progencyDialog.getTxtBxBullNo().setText(_progencyDto.getBullNo());
		//progencyDialog.getTxtFldClfDob().setText(DU.format(_progencyDto.getClfDOB(),DBConstants.DISPLAY_DATE_FORMAT));
		progencyDialog.getDtChoCalfDateOfBirth().setDate(progencyDto.getClfDOB());
		progencyDialog.getTxtBxColor().setText(_progencyDto.getColor());
		progencyDialog.getTxtBxDistrict().setText(_progencyDto.getDistrict());
		progencyDialog.getTxtBxFarmerCode().setText(_progencyDto.getFarmerCode());
		progencyDialog.getTxtBxFarmerName().setText(_progencyDto.getFarmerName());
		progencyDialog.getTxtBxMandal().setText(_progencyDto.getMandal());
		//progencyDialog.getTxtFldGrwmthWgt().setText(_progencyDto.getMonthlyWeight());
		progencyDialog.getTxtNoOfAiDoneConc().setText(_progencyDto.getNoOfAI());
		progencyDialog.getTxtBxNoCalving().setText(_progencyDto.getNoOfCalving());
		progencyDialog.getTxtBxPercentage().setText(_progencyDto.getPercentage());
		progencyDialog.getTxtBxPrsntMilkYldLtrs().setText(_progencyDto.getPresentYield());
		progencyDialog.getTxtBxPrvVactYldLtrs().setText(_progencyDto.getPreviousYield());
		progencyDialog.getTxtBxRelationType().setText(_progencyDto.getRelationShipName());
		progencyDialog.getTxtBxSex().setText(_progencyDto.getSex());
		progencyDialog.getTxtBxTagNo().setText(_progencyDto.getTagNo());
		progencyDialog.getTxtBxVillage().setText(_progencyDto.getVillage());
		progencyDialog.getTxtBxSirBreed().setText(_progencyDto.getDmMilkYield());
		progencyDialog.getTxtBxSiresMilkYieldLtrs().setText(_progencyDto.getSirenBreed());
		
	}

	/**
	 * Method called to read the data from GUI and set them to DTO Object 
	 * @param _guiObject
	 */
	@Override
	public void setGuiDetailsToDto(Object object) {
		// TODO Auto-generated method stub
		final AddProgencyDialog _progencyDialog = (AddProgencyDialog)object;
		progencyDto = new ProgencyDto();
		progencyDto.setCode(_progencyDialog.getTxtFldCode().getText());
		//progencyDto.setBirthWeight(_progencyDialog.getTxtFldGrwBrtWgt().getText());
		progencyDto.setBreed(_progencyDialog.getTxtBxBreed().getText());
		progencyDto.setBreedType(_progencyDialog.getTxtBxBreedType().getText());
		progencyDto.setBullNo(_progencyDialog.getTxtBxBullNo().getText());
		//progencyDto.setClfDOB(_progencyDialog.getTxtFldCalfDOB().getText());
		if(_progencyDialog.getDtChoCalfDateOfBirth().getDate() != null){
			progencyDto.setClfDOB(_progencyDialog.getDtChoCalfDateOfBirth().getDate());
		}
		progencyDto.setColor(_progencyDialog.getTxtBxColor().getText());
		progencyDto.setDistrict(_progencyDialog.getTxtBxDistrict().getText());
		progencyDto.setFarmerCode(_progencyDialog.getTxtBxFarmerCode().getText());
		progencyDto.setFarmerName(_progencyDialog.getTxtBxFarmerName().getText());
		progencyDto.setMandal(_progencyDialog.getTxtBxMandal().getText());
		//progencyDto.setMonthlyWeight(_progencyDialog.getTxtFldGrwmthWgt().getText());
		progencyDto.setNoOfAI(_progencyDialog.getTxtNoOfAiDoneConc().getText());
		progencyDto.setNoOfCalving(_progencyDialog.getTxtBxNoCalving().getText());
		progencyDto.setPercentage(_progencyDialog.getTxtBxPercentage().getText());
		progencyDto.setPresentYield(_progencyDialog.getTxtBxPrsntMilkYldLtrs().getText());
		progencyDto.setPreviousYield(_progencyDialog.getTxtBxPrvVactYldLtrs().getText());
		progencyDto.setRelationShipName(_progencyDialog.getTxtBxRelationType().getText());
		progencyDto.setSex(String.valueOf(_progencyDialog.getVisComBoxModel().getSelectedKey()));
		progencyDto.setTagNo(_progencyDialog.getTxtBxTagNo().getText());
		progencyDto.setVillage(_progencyDialog.getTxtBxVillage().getText());
		progencyDto.setSirenBreed(_progencyDialog.getTxtBxSirBreed().getText());
		progencyDto.setDmMilkYield(_progencyDialog.getTxtBxSiresMilkYieldLtrs().getText());
		progencyDto.setFollowUpDetails(AddProgencyDialog.tempFollowUpLst);

	}
	public void setProgencyDialog1(AddProgencyDialog progencyDialog) {
		this.progencyDialog = progencyDialog;
	}

	public AddProgencyDialog getProgencyDialog() {
		return progencyDialog;
	}

	public void setProgencyCtlr(ProgencyCtlr progencyCtlr) {
		this.progencyCtlr = progencyCtlr;
	}

	public ProgencyCtlr getProgencyCtlr() {
		return progencyCtlr;
	}
	@Override
	public void clearGUILabelColors(Color _lblColor) {
		//set the Field's Labels to default Black color
		progencyDialog.getLblCode().setForeground(_lblColor);
		progencyDialog.getLblBreed().setForeground(_lblColor);
		progencyDialog.getLblBreedType().setForeground(_lblColor);
		progencyDialog.getLblBullNo().setForeground(_lblColor);
		progencyDialog.getLblCalfDob().setForeground(_lblColor);
		progencyDialog.getLblMandal().setForeground(_lblColor);
		progencyDialog.getLblColor().setForeground(_lblColor);
		//progencyDialog.getLblDeticking().setForeground(_lblColor);
		//progencyDialog.getLblDeWorming().setForeground(_lblColor);
		progencyDialog.getLblDistrict().setForeground(_lblColor);
		progencyDialog.getLblFarmerCode().setForeground(_lblColor);
		progencyDialog.getLblFarmerName().setForeground(_lblColor);
	//	progencyDialog.getLblBrwBrtWgt().setForeground(_lblColor);
		//progencyDialog.getLblGrwMthWgt().setForeground(_lblColor);
		progencyDialog.getLblNoAIDoneConc().setForeground(_lblColor);
		progencyDialog.getLblNoOfCalving().setForeground(_lblColor);
		//progencyDialog.getLblNutrition().setForeground(_lblColor);
		//progencyDialog.getLblOthr().setForeground(_lblColor);
		progencyDialog.getLblPercentage().setForeground(_lblColor);
		progencyDialog.getLblPrevVacYldLtrs().setForeground(_lblColor);
		progencyDialog.getLblPrsntMlkYldLtrs().setForeground(_lblColor);
		progencyDialog.getLblRelationtype().setForeground(_lblColor);
		progencyDialog.getLblSex().setForeground(_lblColor);
		progencyDialog.getLblTagNo().setForeground(_lblColor);
		//progencyDialog.getLblVaccination().setForeground(_lblColor);
		progencyDialog.getLblVillage().setForeground(_lblColor);


	}


}//end class
